package leader.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import teacher.bean.Paper;
import teacher.bean.Patent;
import teacher.bean.Teacher;
import tools.StringDate;
import database.dao.MysqlCrud;

public class DirectPatentService {
	/**
	 * @category 查询同一研究方向的所有专利
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Patent> 返回以List<Patent>的专利集合
	 */	
	public List<Patent> selectAllDirectPatent(Teacher teacher) {
		List<Patent> lists = new ArrayList<Patent>();
		String sql="select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.examine<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有专利
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Patent> 返回以List<Patent>的专利集合
	 */	
	public List<Patent> selectSegregateAllDirectPatent(Teacher teacher,int pageNow,int pageSize) {
		List<Patent> lists = new ArrayList<Patent>();
		String sql="select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.examine<>? and patId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过名称模糊查询一个研究方向的一篇专利
	 * @param vague 查询的专利的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Paper> 返回一个List<Paper>的专利集合
	 */
	public List<Patent> selectPatentLikeName(String vague,Teacher leader) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.name like '%"+vague+"%' and T.direction=? and acheve.examine<>? ";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Patent> lists=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过名称模糊查询一个研究方向的一篇专利
	 * @param vague 查询的专利的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Paper> 返回一个List<Paper>的专利集合
	 */
	public List<Patent> selectSegregatePatentLikeName(String vague,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.name like '%"+vague+"%' and T.direction=? and acheve.examine<>? and patId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Patent> lists=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过时间段查询方向专利
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return PaperOne List<Patent> 返回查询到的List<Patent>专利列表
	 */
	public List<Patent> selectDirectPatentByTime(Date date1,Date date2,Teacher leader) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve,teacher as T where acheve.teaId=T.teaId and empowerDate>='"+date1+"' and empowerDate<='"+date2+"' and T.direction=? and acheve.examine<>?";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Patent> lists=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过时间段查询方向专利
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return PaperOne List<Patent> 返回查询到的List<Patent>专利列表
	 */
	public List<Patent> selectSegregateDirectPatentByTime(Date date1,Date date2,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve,teacher as T where acheve.teaId=T.teaId and empowerDate>='"+date1+"' and empowerDate<='"+date2+"' and T.direction=? and acheve.examine<>? and patId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Patent> lists=new ArrayList<Patent>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	/**
	 * @category 专利的审核
	 * @author chenkai
	 * @param paper Paper 传入审核的专利以及审核结果
	 * @return flag boolean
	 */
	public boolean updatePatentState(Patent patent) {
		boolean flag = false;
		String sql="update patent set examine = ? where patId = ?";
		String[] parameter={
				String.valueOf(patent.getExamine()),
				String.valueOf(patent.getPatId())
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 查询同一研究方向的所有专利
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Patent> 返回以List<Patent>的专利集合
	 */	
	public List<Patent> selectSegregateDirectPatentByState(Teacher teacher,Patent pat,int pageNow,int pageSize) {
		List<Patent> lists = new ArrayList<Patent>();
		String sql="select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.examine=? and T.direction=? and acheve.examine<>? and patId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(pat.getExamine()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一研究方向的所有专利
	 * @author chenkai
	 * @param teacher Teacher 研究方向信息
	 * @return lists List<Patent> 返回以List<Patent>的专利集合
	 */	
	public List<Patent> selectDirectPatentByState(Teacher teacher,Patent pat) {
		List<Patent> lists = new ArrayList<Patent>();
		String sql="select * from (select * from patent order by examine,expClass,empowerDate desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.examine<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToPatent(map));
		}
		return lists;
	}
	
	/**
	 * @category 得到一个方向的所有专利的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPatent(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from patent as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.examine<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有专利的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPatentByTime(Teacher teacher,Date date1,Date date2) {
		int sum=0;
		String sql="select count(*) from patent as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.examine<>? and empowerDate>='"+date1+"' and empowerDate<='"+date2+"'";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有专利的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPatentByState(Teacher teacher,Patent pat) {
		int sum=0;
		String sql="select count(*) from patent as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.examine = ? and T.direction=? and acheve.examine<>?";
		String[] parameter={
				String.valueOf(pat.getExamine()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有专利的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectPatentByName(Teacher teacher,String vague) {
		int sum=0;
		String sql="select count(*) from patent as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.name like '%"+vague+"%' and T.direction=? and acheve.examine<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
}
